﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using System.IO;

namespace WWInternal
{
    class WWUtils
    {
        public static void ExportAsPng(RenderTexture t, string filename, TextureFormat fmt)
        {
            var lastRT = RenderTexture.active;
            RenderTexture.active = t;
            Rect rect = new Rect(0, 0, t.width, t.height);
            Texture2D tmp_texture_2d = new Texture2D(t.width, t.height, fmt, false);
            tmp_texture_2d.ReadPixels(rect, 0, 0);
            
            var res = ImageConversion.EncodeToPNG(tmp_texture_2d);
            RenderTexture.active = lastRT;
            FileStream fs = new FileStream(filename, FileMode.Create);
            fs.Write(res, 0, res.Length);
            fs.Close();
        }

        public static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp = lhs;
            lhs = rhs;
            rhs = temp;
        }
    }

}